<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-25 20:19:21
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-25 20:22:43
 * @Descripttion: 
 */


namespace app\common\model;

use think\Model;

class BusGoodsCart extends Base
{

    // public function getMoneyAttr($value,$data){
    //     $goods_money = (new \app\common\model\GoodsSku())->where(['id' => $data['goods_sku_id']])->value('money');
        
    //     $cart = (new \app\common\model\GoodsCart())->where(['id' => $data['id']])->find();

    //     if($cart['user_coupon_id'] == 0){
    //         $cart_coupon_id = (new \app\common\model\GoodsCart())
    //                     ->where(['user_id' => $data['user_id']])
    //                     ->where('user_coupon_id','<>',0)->column('user_coupon_id');
            
    //         $goods_user_coupon_3 = (new \app\common\model\UserCoupon())
    //                 ->where(['user_id' => $data['user_id'],'goods_id' => $data['goods_id'],'type' => 3,'state' => 0])
    //                 ->whereNotIn('id',$cart_coupon_id)
    //                 ->where('edntime','>',time())->find();
    //         if($goods_user_coupon_3){
    //             $money = ($goods_money-$goods_user_coupon_3['money']) >= 0 ? $goods_money-$goods_user_coupon_3['money'] : $goods_money;
    //             if($money != 0){
    //                 (new \app\common\model\GoodsCart())->where(['id' => $data['id']])->update([
    //                     'user_coupon_id' => $goods_user_coupon_3['id']
    //                 ]);
    //             }
    //         }
            
    //     }
    //     $cart = (new \app\common\model\GoodsCart())->where(['id' => $data['id']])->find();
    //     #指定商品
    //     $goods_user_coupon = (new \app\common\model\UserCoupon())
    //                     ->where(['user_id' => $data['user_id'],'goods_id' => $data['goods_id'],'type' => 3,'state' => 0,'id' => $cart['user_coupon_id']])
    //                     ->where('edntime','>',time())->find();

    //     if($goods_user_coupon && $cart['user_coupon_id'] > 0){
    //         $money = ($goods_money-$goods_user_coupon['money']) >= 0 ? $goods_money-$goods_user_coupon['money'] : $goods_money;
    //         return [
    //             'goods_money' =>  sprintf('%.2f',$money),
    //             'coupon_money' => $goods_user_coupon['money'],
    //             'money_cost' => $goods_money,
    //         ];
    //     }
   

    //     return (new \app\common\model\GoodsSku())->where(['id' => $data['goods_sku_id']])->value('money');
    // }
    
    public function getMoneyArrAttr($value,$data){
        $cart = (new \app\common\model\BusGoodsCart())
                ->alias('cart')
                ->join("{$this->tdb}agent_goods_sku sku","sku.id = cart.goods_sku_id")
                ->where('cart.id',$data['id'])
                ->find();
        $money = $cart['money']*$cart['num'];
        $user_coupon = (new \app\common\model\UserCoupon())
                ->where(['id' => $cart['user_coupon_id']])
                ->field(['id','title','money','bus_id','addtime','edntime','full_money','type','goods_id'])
                ->append(['addtime_text','edntime_text'])
                ->hidden(['addtime','edntime'])
                ->find();
    
        $goods_money = $money;$coupon_money = 0;
        switch (!empty($user_coupon['type'])) {
            case 3:
                if($money > $user_coupon['money'] && $user_coupon['goods_id'] == $cart['goods_id']){
                    $goods_money = $money-$user_coupon['money'];
                    $coupon_money = $user_coupon['money'];
                }else{
                    $goods_money = $money;
                }
                break;
            case 2:
                if($money > $user_coupon['full_money'] && $user_coupon['money'] < $money){
                    $goods_money = $money-$user_coupon['money'];
                    $coupon_money = $user_coupon['money'];
                }else{
                    $goods_money = $money;
                }
                break;
            case 1:
                if($money > $user_coupon['money']){
                    $goods_money = $money-$user_coupon['money'];
                    $coupon_money = $user_coupon['money'];
                }else{
                    $goods_money = $money;
                }
                break;
        }
        return [
            'goods_money' => sprintf('%.2f',$goods_money),
            'coupon_money' => sprintf('%.2f',$coupon_money),
            'money_cost' => sprintf('%.2f',$money),
            'd_money' => sprintf('%.2f',$cart['money']),
        ];
        
    }


    // public function getCouponMoneyAttr($value,$data){
    //         $money = (new \app\common\model\GoodsSku())->where(['id' => $data['goods_sku_id']])->value('money');
    //         #指定商品
    //         $goods_user_coupon_3 = (new \app\common\model\UserCoupon())
    //                             ->where(['user_id' => $data['user_id'],'goods_id' => $data['goods_id'],'type' => 3,'state' => 0])
    //                             ->where('edntime','>',time())->find();

    //         if($goods_user_coupon_3){
    //             $money = ($money-$goods_user_coupon_3['money']) >= 0 ? $money-$goods_user_coupon_3['money'] : $money;
    //             return sprintf('%.2f',$goods_user_coupon_3['money']);
    //         }
    //     return '0.00';
    // }
    // public function getGoodsMoneyCostAttr($value,$data){
    //     $money = (new \app\common\model\GoodsSku())->where(['id' => $data['goods_sku_id']])->value('money');
    //     return $money;
    // }

    public function getGoodsTitleAttr($value,$data){
        return (new \app\common\model\AgentGoods())->where(['id' => $data['goods_id']])->value('title');
    }

    public function getGoodsSkuTitleAttr($value,$data){
        return (new \app\common\model\AgentGoodsSku())->where(['id' => $data['goods_sku_id']])->value('sku_title');
    }

    public function getGoodsImgAttr($value,$data){
        $array = (new \app\common\model\AgentGoods())->where(['id' => $data['goods_id']])->value('image');
        $array = empty($array) ? [] : json_decode($array, true);
        return $array;
    }
    public function getCoverImageTextAttr($value,$data){
        return (new \app\common\model\Goods())->where(['id' => $data['goods_id']])->value('cover_image');
      
    }
    public function getWarehouseAttr($value,$data){
        $order = ['distance' => 'asc']; // 按距离排序
        $this->lat = '24.88513100'; // 用户经度
        $this->lon = '102.82413200';//用户纬度
        $data = (new \app\common\model\Warehouse())
                ->field("title,(6378.138 * 2 * asin(sqrt(pow(sin((latitude * pi() / 180 - " . $this->lat . " * pi() / 180) / 2),2) + cos(latitude * pi() / 180) * cos(" . $this->lat . " * pi() / 180) * pow(sin((longitude * pi() / 180 - " . $this->lon . " * pi() / 180) / 2),2))) * 1000) as distance")
                // ->where(['agent_id' => $data['agent_id']])
                ->order('distance asc')
                ->find();
       return $data;
    }
}
